<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;

class Tinetgroup extends Common
{

	//列表
    public function index()
    {

        $where = [];

        $title = $this->request->get('title');
        $this->assign('title',$title);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        if($start&&$end){
            $where['ctime'] = ['between',[strtotime($start),strtotime($end)]];
        }elseif($start){
            $where['ctime'] = ['>',strtotime($start)];
        }elseif($end){
            $where['ctime'] = ['<',strtotime($end)];
        }
        $count = Db::table('cg_ti_net_group t')->where($where)->count();
        $data = Db::table('cg_ti_net_group t')->order("t.id desc")->where($where)->paginate(10,$count);

//        echo Db::table('cg_ti')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);

        return $this->fetch();
    }


    public function add(){

        if($_POST){

            $validate = new Valid([
                'title|分组名称'  => 'require|max:200',
                'jxs_id|经销商'  => 'require',
                'net_id|网点'  => 'require',
            ]);
            $data = [];
            $data['title'] = $this->request->post('title');
            $data['jxs_id'] = $this->request->post('jxs_id');
            $data['net_id'] = $this->request->post('net_id/a');
            if($data['net_id']){
                $data['net_id'] = serialize($data['net_id']);
            }
            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['cuser'] = $_SESSION['think']['employee_id'];
            $data['ctime'] = time();
            $data['ptime'] = time();

            $bool = Db::table('cg_ti_net_group')->insert($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"添加成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"添加失败"
                ]);die;
            }

        }

        //查询雇员用户名
        $em_name = db('employee')->where('id',Session::get('employee_id'))->value('em_name');
        $this->assign('em_name',$em_name);

        return $this->fetch('form');
    }

    /**
     * 修改基本信息
     * @return mixed
     * @throws \think\Exception
     */
    public function update(){
        $id = $this->request->param('id');

        if($_POST){

            $validate = new Valid([
                'title|分组名称'  => 'require|max:200',
                'jxs_id|经销商'  => 'require',
                'net_id|网点'  => 'require',
            ]);
            $data = [];
            $data['title'] = $this->request->post('title');
            $data['jxs_id'] = $this->request->post('jxs_id');
            $data['net_id'] = $this->request->post('net_id/a');
            if($data['net_id']){
                $data['net_id'] = serialize($data['net_id']);
            }
            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['cuser'] = $_SESSION['think']['employee_id'];
            $data['ctime'] = time();
            $data['ptime'] = time();

            $bool = Db::table('cg_ti_net_group')->where('id',$id)->update($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"修改成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"修改失败"
                ]);die;
            }

        }

        $data = db('cg_ti_net_group')->where('id',$id)->find();
        $data['jxs_title'] = db('cg_jxs')->where('id',$data['jxs_id'])->value('title');
        $data['net_id'] = unserialize($data['net_id']);
        foreach($data['net_id'] as $k=>$v){
            $data['net_id'][$k] = [
                'id'=>$v,
                'title'=>db('cg_net')->where('id',$v)->value('title')
            ];
        }
        $this->assign('data',$data);

        //查询雇员用户名
        $em_name = db('employee')->where('id',Session::get('employee_id'))->value('em_name');
        $this->assign('em_name',$em_name);

        return $this->fetch('form');
    }

    /**
     * 删除
     * @throws \think\Exception
     */
    public function delete(){
        $id = $this->request->param('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_ti_net_group')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }

    //选择经销商
    public function selectJXS(){
        $this->assign('id',1);
        return $this->fetch();
    }

    //选择网点
    public function selectNet(){
        return $this->fetch();
    }
    //保存为常用分组
    public function saveAsCommonNetGroup(){

        $data = [];
        $data['title'] = date('YmdHis');
        $data['jxs_id'] = $this->request->post('jxs_id');
        $data['net_id'] = $this->request->post('net_ids/a');
        if($data['net_id']){
            $data['net_id'] = serialize($data['net_id']);
        }

        $data['cuser'] = $_SESSION['think']['employee_id'];
        $data['ctime'] = time();
        $data['ptime'] = time();

        $bool = Db::table('cg_ti_net_group')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"添加成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"添加失败"
            ]);die;
        }
    }
}
